package com.zhiyou100.controller;

import cn.hutool.core.util.ObjectUtil;
import com.zhiyou100.entity.ResponseEntity;
import com.zhiyou100.entity.Student;
import com.zhiyou100.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 学生 控制
 * 表：1  学生表：名字  邮箱 电话号码  ：
 * 注册  登陆  添加成绩  查询成绩
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-09-26 20:39
 */
@Controller
@ResponseBody
public class StudentAction {
    private StudentService service;

    @Autowired
    public void setService(StudentService service) {
        this.service = service;
    }

    /**
     * 注册一个学生，接受 json 对象
     *
     * @param student 要添加的对象
     * @return 响应
     */
    @PostMapping("/student/add.action")
    public ResponseEntity<Student> addOne(Student student) {
        if (ObjectUtil.isNull(student) || ObjectUtil.isEmpty(student)) {
            // 对象为空
            return new ResponseEntity<>(500, "添加失败！参数为空", null);
        }
        if (ObjectUtil.emptyCount(student) > 1 || !ObjectUtil.isNull(student.getId())) {
            // 为 null 的对象数量大于 1，或者 为 null 的不是 id
            return new ResponseEntity<>(500, "添加失败！参数不合法", student);
        }
        if (service.getOne(student.getId()) != null) {
            // 数据库中已经存在该 id
            return new ResponseEntity<>(500, "添加失败！已经存在该 id", student);
        }
        if (service.selectOneByName(student.getName()) != null) {
            // 数据库中已经存在该 name
            return new ResponseEntity<>(500, "添加失败！已经存在该 name", student);
        }
        String email = "email";
        if (service.selectOneByField(email, student.getEmail()) != null) {
            // 数据库中已经存在该 email
            return new ResponseEntity<>(500, "添加失败！已经存在该 email", student);
        }
        String phone = "phone";
        if (service.selectOneByField(phone, student.getPhone()) != null) {
            // 数据库中已经存在该 phone
            return new ResponseEntity<>(500, "添加失败！已经存在该 phone", student);
        }
        service.addOne(student);
        return new ResponseEntity<>(200, "添加成功！", student);
    }

    /**
     * 登陆
     * RequestBody 接受 json
     * @param from 参数，接受 json
     * @return 响应
     */
    @PostMapping(value = "/student/login.action")
    public ResponseEntity<Student> login(@RequestBody Student from, HttpServletRequest request) {
        if (ObjectUtil.isNull(from) || ObjectUtil.isNull(from.getId()) || ObjectUtil.isNull(from.getPassword())) {
            // 参数为空，或者 id、password 为空
            return new ResponseEntity<>(500, "登陆失败！参数为空，或者 id、password 为空", from);
        }
        final Student student = service.selectOneByIdAndPwd(from.getId(), from.getPassword());
        if (ObjectUtil.isNull(student)) {
            // 用户名或者密码错误
            return new ResponseEntity<>(500, "登陆失败！用户名或者密码错误", student);
        } else {
            request.getSession().setAttribute("student", student);
            return new ResponseEntity<>(200, "登陆成功！", student);
        }
    }


}
